package com.wallet.trans.resp;

import com.wallet.trans.enums.ResultCodeEnum;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BaseResp<T> {

    private Integer code;

    private String message;

    private T data;


    public static <T> BaseResp<T> ok(T data){
        BaseResp<T> baseResp = new BaseResp<>();
        baseResp.code = ResultCodeEnum.PASS.getCode();
        baseResp.message = ResultCodeEnum.PASS.getMessage();
        baseResp.data = data;
        return baseResp;
    }

    public static <T> BaseResp<T> ok(){
        return ok(null);
    }

    public static <T> BaseResp<T> ok( Integer code,String message ,T data){
        BaseResp<T> baseResp = new BaseResp<>();
        baseResp.code = code;
        baseResp.message = message;
        baseResp.data = data;
        return baseResp;
    }

    public static <T> BaseResp<T> error(Integer code,String message){
        BaseResp<T> baseResp = new BaseResp<>();
        baseResp.code = code;
        baseResp.message = message;
        return baseResp;
    }

    /**
     * 参数错误异常
     * @param message 说明
     */
    public static <T> BaseResp<T> paramError(String message){
        BaseResp<T> baseResp = new BaseResp<>();
        baseResp.code = ResultCodeEnum.PARAMS_ERROR.getCode();
        baseResp.message = message;
        return baseResp;
    }

    public static <T> BaseResp<T> error(Integer code){
        String message = ResultCodeEnum.getInstance(code).getMessage();
        return error(code,message);
    }

    public static <T> BaseResp<T> error(){
        return error(ResultCodeEnum.OPERATION_ERROR.getCode());
    }
    public static <T> BaseResp<T> error(ResultCodeEnum resultCodeEnum){
        BaseResp<T> baseResp = new BaseResp<>();
        baseResp.code = resultCodeEnum.getCode();
        baseResp.message = resultCodeEnum.getMessage();
        return baseResp;
    }

    public static <T> BaseResp<T> status(boolean success){
        return success ? ok() : error();
    }

}
